# curl -s http://wuyou.run/scripts/system/jys/yace.sh |bash -s set-hostname
###
 # @Author: Logan.Li
 # @Gitee: https://gitee.com/attacker
 # @email: admin@attacker.club
 # @Date: 2025-01-01 18:21:50
 # @LastEditTime: 2025-01-01 18:46:10
 # @Description: 
### 

if [ $(id -u) -ne 0 ]; then
    color_message "warn" ">> This script must be run as root !!!"
    exit 0
fi

# -----------  打印颜色 ----------- #
function color_message() {
    case "$1" in
    "error" | "red")
        echo -e "\e[1;31m$2\e[0m"
        ;;
    "warn" | "yellow")
        echo -e "\e[1;33m$2\e[0m"
        ;;
    "info" | "blue")
        echo -e "\e[1;34m$2\e[0m"
        ;;
    "success" | "green")
        echo -e "\e[1;32m$2\e[0m"
        ;;
    esac
}



Set_hostname() {
    # bash host_init.sh hostname 主机名传参
    if [ $# -lt 1 ]; then
        #传参少于1个
        color_message "warn" "---- Use default options  ----"
        HOSTNAME="TemplateOS"
        # 默认主机名TemplateOS
    else
        color_message "info" "---- Set  Hostname ----"
        HOSTNAME=$1
    fi

    if [ -f /etc/hostname ]; then
        echo "$HOSTNAME" >/etc/hostname
    fi
    sed -i "/HOSTNAME/c HOSTNAME=$HOSTNAME" /etc/sysconfig/network || echo "HOSTNAME=$HOSTNAME" >>/etc/sysconfig/network
    hostname $HOSTNAME
    grep $HOSTNAME /etc/hosts || echo "127.0.0.1 $HOSTNAME" >>/etc/hosts
}



Set_profile() {
    color_message "info" "---- Start configuring  /etc/profile ---- "
    grep PS /etc/profile || echo '''PS1="\[\e[37;1m\][\[\e[32;1m\]\u\[\e[37;40m\]@\[\e[34;1m\]\h \[\e[0m\]\t \[\e[35;1m\]\W\[\e[37;1m\]]\[\e[m\]/\\$" ''' >>/etc/profile
    grep HISTTIMEFORMAT /etc/profile || cat >>/etc/profile <<EOF

export HISTTIMEFORMAT="%F %T \$(whoami) " 
export HISTSIZE=10000 
EOF
}


Push_key() {
    color_message "info" "---- 推送ssh管理key ----"
    echo > /root/.ssh/authorized_keys
    su - root -c 'mkdir -p /root/.ssh'
    grep "manage.pem" -lr /root/.ssh/authorized_keys || cat >>/root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWtww3XnLfAJ9aEhlWxxyMG1w2z887z78JxQd8Lg80oEiWVBV6z4EVx5PXsgRhDC6vmv+KnNctAJDOpAjQ8e63rnQzMItH806halnkPJ8qU/ZPpVVCzbRl5ZY7o7ePbmwNv/nNqdUNLkyTID1xzIhnmMtEmkSNAgkbcSp3njqgVmtkED+kfmpvEawn3M5kt1NDL5KtyleDEyBDJ6Vg7CIt5uQfH7cnPPksg0AYhA8OOkIwO9I0tCOLkeKFb778s/NUHJ9EPbG+GbmXJ/j0SdFsVYZiaeOqL+x5sJIF1cpGzDCrxGkrgK9WTquQltj43sFvoOjg0R2L//qdkJ604961 # manage.pem
EOF
    chown root.root /root/.ssh/authorized_keys
    chmod 700 /root/.ssh
    chmod 600 /root/.ssh/authorized_keys
}







Init_Install() {
    
    Set_hostname $1

    Set_profile
    Push_key
}

Init_Install $1